<?php
require 'db.php'; // 包含数据库连接
require 'vendor/autoload.php'; // 引入 Composer 自动加载器
include_once 'secret/jwt.php';

use Firebase\JWT\JWT;
use Firebase\JWT\Key;

// 获取 JWT
if (!isset($_COOKIE['token'])) {
    header('Location: index.html');
    exit();
}

$jwt = $_COOKIE['token'];

try {
    $decoded = JWT::decode($jwt, new Key($secretKey, 'HS256'));
    $username = $decoded->data->username;
} catch (Exception $e) {
    echo json_encode(['error' => '无效的令牌: ' . $e->getMessage()]);
    exit();
}

// 从数据库获取用户个人资料，包括头像路径
$stmt = $conn->prepare("SELECT * FROM users WHERE username = ?");
$stmt->bind_param("s", $username);
$stmt->execute();
$result = $stmt->get_result();
$user = $result->fetch_assoc();
$stmt->close();

// 设置默认头像路径
$defaultAvatar = 'uploads/hhh.jpg';

// 获取用户头像路径
$avatarPath = !empty($user['avatar_path']) ? $user['avatar_path'] : $defaultAvatar;

$phone = $user['phone'];

// 将头像路径传递给前端
$data = [
    'username' => htmlspecialchars($username),
    'avatarPath' => htmlspecialchars($avatarPath),
    'phone' => htmlspecialchars($phone)
];
echo json_encode($data);
?>
